package dragon.resource;

import dragon.security.handler.AuthenSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
//下面的注解，开启后，oauth的授权码模式会失效，不开启的话，登录返回token会失效
@EnableResourceServer
public class ResourceConfigurer extends ResourceServerConfigurerAdapter {

    @Autowired
    AuthenSuccessHandler authenSuccessHandler;

//    @Autowired
//    AuthenFailHandler authenFailHandler;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login/process")
                .successHandler(authenSuccessHandler)
//                .failureHandler(authenFailHandler)
                .and()
                .authorizeRequests()
                .antMatchers("/staff")
                .hasAnyRole("admin","staff")
//                .antMatchers("/signup","/me")
//                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .cors().disable();
    }
}
